import java.io.*;
import java.util.*;
import javax.microedition.rms.*;

public final class bl {

	private String a;
	private int b;
	private boolean c;
	private boolean d;
	private RecordStore e[];
	private boolean f[];
	private Hashtable g;
	private long h;
	private bv i;

	public bl(String s) {
		e = new RecordStore[4];
		f = new boolean[4];
		g = new Hashtable();
		a = s;
		e();
	}

	private void e() {
		if (!d) {
			d = true;
			byte abyte0[];
			if ((abyte0 = b(0, 1)) != null) {
				c = true;
				DataInputStream datainputstream = new DataInputStream(
						new ByteArrayInputStream(abyte0));
				try {
					datainputstream.readUnsignedShort();
					int j = datainputstream.readInt();
					for (int k = 0; k < j; k++) {
						bc bc1 = new bc(datainputstream.readUTF());
						bc1.a = datainputstream.readUnsignedByte();
						bc1.b = datainputstream.readInt();
						g.put(bc1.c, bc1);
						b = Math.max(b, bc1.a);
					}

					datainputstream.close();
				} catch (IOException ioexception) {
					ce.c(a + ": " + ioexception);
					return;
				}
			} else {
				g();
				c = true;
			}
		}
	}

	private void f() {
		h = System.currentTimeMillis();
		if (i == null)
			bb.c.schedule(i = new bv(this), 2000L);
	}

	private void g() {
		byte abyte0[];
		Enumeration elem;
		synchronized (this) {
			if (d) {
				if (System.currentTimeMillis() - h < 2000L) {
					bb.c.schedule(i = new bv(this), 2000L);
					return;
				}
				i = null;
				e e1 = new e(1024);
				try {
					DataOutputStream dataoutputstream = e1.c();
					dataoutputstream.writeShort(52973);
					dataoutputstream.writeInt(g.size());
					bc bc1;
					for (elem = g.elements(); elem.hasMoreElements(); dataoutputstream
							.writeInt(bc1.b)) {
						bc1 = (bc) elem.nextElement();
						dataoutputstream.writeUTF(bc1.c);
						dataoutputstream.writeByte(bc1.a);
					}

					dataoutputstream.close();
				} catch (IOException _ex) {
				}
				abyte0 = e1.a();
				if (c)
					a(0, 1, abyte0, abyte0.length);
				else
					a(0, abyte0, abyte0.length);
			}
		}
	}

	private void a(String s, byte abyte0[], int j) {
		e();
		int k;
		synchronized (this) {
			bc bc1 = (bc) g.get(s);
			if (bc1 == null) {
				bc1 = new bc(s);
				g.put(s, bc1);
			}
			k = -1;
			if (bc1.b > 0) {
				k = bc1.a;
				if (a(bc1.a, bc1.b, abyte0, j))
					return;
				a(bc1.a, bc1.b);
				bc1.a = 0;
			}
			for (; bc1.a < 32; bc1.a++) {
				if (bc1.a != k && (bc1.a >= f.length || !f[bc1.a])) {
					bc1.b = a(bc1.a, abyte0, j);
					if (bc1.b > 0)
						break;
					if (c(bc1.a) == 0) {
						bt.d().a(6, false);
						return;
					}
				}
			}
			f();
		}
	}

	public final void a(String s, byte abyte0[]) {
		a(s, abyte0, abyte0.length);
	}

	public final void a(String s, e e1) {
		if (s != null && e1 != null)
			a(s, e1.a(), e1.b());
	}

	public final void a(String s, q q1) {
		if (s != null && q1 != null) {
			e e1 = q1.j();
			a(s, e1.a(), e1.b());
		}
	}

	public final void a(String s) {
		e();
		bc bc1;
		synchronized (this) {
			if ((bc1 = (bc) g.remove(s)) != null) {
				a(bc1.a, bc1.b);
				f();
			}
		}
	}

	public final void a() {
		e();
		synchronized (this) {
			g.clear();
			for (int j = 0; j <= b; j++)
				try {
					a(j);
					RecordStore.deleteRecordStore(a + "." + j);
				} catch (RecordStoreException _ex) {
				}

		}
	}

	public final int b() {
		e();
		int j = 0;
		for (int k = 0; k <= b; k++) {
			RecordStore recordstore;
			try {
				if ((recordstore = b(k)) != null)
					j += recordstore.getSize();
			} catch (RecordStoreException _ex) {
			}
		}

		return j;
	}

	public final boolean b(String s) {
		e();
		bl bl1 = this;
		synchronized (bl1) {
			return g.containsKey(s);
		}
	}

	public final byte[] c(String s) {
		e();
		bl bl1 = this;
		synchronized (bl1) {
			bc bc1 = (bc) g.get(s);
			if (bc1 != null)
				return b(bc1.a, bc1.b);
			return null;
		}
	}

	public final DataInputStream d(String s) {
		byte abyte0[] = c(s);
		if (abyte0 != null)
			return new DataInputStream(new ByteArrayInputStream(abyte0));
		else
			return null;
	}

	public final q e(String s) {
		DataInputStream datainputstream = d(s);
		try {
			if ((datainputstream) != null) {
				return q.a(datainputstream);
			}
		} catch (IOException ioexception) {
			ce.a(s, ioexception);
		}
		return null;
	}

	public final cc c() {
		e();
		cc cc1 = new cc();
		synchronized (this) {
			for (Enumeration enumeration = g.keys(); enumeration
					.hasMoreElements(); cc1.e(enumeration.nextElement()))
				;
		}
		return cc1;
	}

	private void a(int j, RecordStoreException recordstoreexception) {
		ce.c(a + "[" + j + "]: " + recordstoreexception);
		a(j);
	}

	private void a(int j) {
		synchronized (this) {
			if (j < e.length && e[j] != null) {
				try {
					e[j].closeRecordStore();
				} catch (RecordStoreException _ex) {
					e[j] = null;
				} finally {
					e[j] = null;
				}
			}
		}
	}

	private RecordStore b(int j) {
		return a(j, true);
	}

	private RecordStore a(int j, boolean flag) {
		e();
		bl bl1 = this;
		synchronized (bl1) {
			if (j >= e.length) {
				RecordStore arecordstore[] = new RecordStore[j + 4];
				System.arraycopy(e, 0, arecordstore, 0, e.length);
				boolean aflag[] = new boolean[j + 4];
				System.arraycopy(f, 0, aflag, 0, f.length);
				e = arecordstore;
				f = aflag;
			}
			if (e[j] == null)
				try {
					b = Math.max(b, j);
					e[j] = RecordStore.openRecordStore(a + "." + j, flag);
				} catch (RecordStoreException recordstoreexception) {
					a(j, recordstoreexception);
				}
			return e[j];
		}
	}

	private int c(int j) {
		int k;
		RecordStore recordstore = null;
		try {
			recordstore = b(j);
			if ((recordstore) != null) {
				k = recordstore.getNumRecords();
				return k;
			}
		} catch (RecordStoreException recordstoreexception) {
			a(j, recordstoreexception);
		} finally {
			if (recordstore != null) {
				try {
					recordstore.closeRecordStore();
				} catch (RecordStoreException e1) {
					e1.printStackTrace();
				}
			}
			recordstore = null;
		}
		return 0;
	}

	private void a(int j, int k) {
		RecordStore recordstore = null;
		try {
			recordstore = b(j);
			if ((recordstore) != null) {
				recordstore.deleteRecord(k);
				f[j] = false;
			}
		} catch (InvalidRecordIDException _ex) {
		} catch (RecordStoreException recordstoreexception) {
			a(j, recordstoreexception);
		}
	}

	private byte[] b(int j, int k) {
		byte abyte0[];
		RecordStore recordstore = null;
		try {
			recordstore = b(j);
			if ((recordstore) != null) {
				abyte0 = recordstore.getRecord(k);
				return abyte0;
			}
		} catch (InvalidRecordIDException _ex) {
		} catch (RecordStoreException recordstoreexception) {
			a(j, recordstoreexception);
		}
		return null;
	}

	private boolean a(int j, int k, byte abyte0[], int l) {
		RecordStore recordstore = null;
		try {
			recordstore = b(j);
			if ((recordstore) != null) {
				recordstore.setRecord(k, abyte0, 0, l);
				return true;
			}

		} catch (InvalidRecordIDException _ex) {
		} catch (RecordStoreFullException recordstorefullexception) {
			f[j] = true;
			a(j, recordstorefullexception);
		} catch (RecordStoreException recordstoreexception) {
			a(j, recordstoreexception);
		}
		return false;
	}

	private int a(int j, byte abyte0[], int k) {
		int l;
		RecordStore recordstore = null;
		try {
			recordstore = b(j);
			if ((recordstore) != null) {
				l = recordstore.addRecord(abyte0, 0, k);
				return l;
			}
		} catch (InvalidRecordIDException _ex) {
		} catch (RecordStoreFullException recordstorefullexception) {
			f[j] = true;
			a(j, ((RecordStoreException) (recordstorefullexception)));
		} catch (RecordStoreException recordstoreexception) {
			a(j, recordstoreexception);
		}
		return 0;
	}

	public static void a(String as[]) {
		String as1[] = RecordStore.listRecordStores();
		if (as1 != null) {
			int j = 0;
			for (int k = as1.length; j < k; j++) {
				boolean flag = false;
				if (as != null) {
					String s = as1[j];
					int l = 0;
					int i1 = as.length;
					do {
						if (l >= i1)
							break;
						if (s.startsWith(as[l])) {
							flag = true;
							break;
						}
						l++;
					} while (true);
				}
				if (flag)
					continue;
				try {
					RecordStore.deleteRecordStore(as1[j]);
				} catch (RecordStoreException _ex) {
				}
			}

		}
	}

	public static int d() {
		int j = 0;
		String as[] = RecordStore.listRecordStores();
		if (as != null) {
			int k = 0;
			for (int l = as.length; k < l; k++) {
				RecordStore recordstore;
				try {
					if ((recordstore = RecordStore
							.openRecordStore(as[k], false)) != null) {
						j += recordstore.getSize();
						recordstore.closeRecordStore();
					}
				} catch (RecordStoreException _ex) {
				}
			}

		}
		return j;
	}

	public static void a(bl bl1) {
		bl1.g();
	}
}